(function() {
    if (!window["vg"]) {
        vg = {};
    }

    if (!vg.desktop) {
        vg.desktop = {};
    }

    var identifiers = {
        "desktopFrame" : "desktop",
        "breadcrumFrame" : "breadcrum",
        "headerDiv" : "header",
        "appLogo" : "applicationLogo",
        "appTitle" : "applicationTitle",
        "changeApp" : "Click here to change Application",
        "userName" : "loginName",
        "profile" : "View Profile",
        "logOut" : "Log Out",
        "myProfileDiv" : "myProfileDiv"
    }

    var xpaths = {
        "Application" : {
            "id" : ".//*[local-name() = 'APPLICATIONID']",
            "name" : ".//*[local-name() = 'APPLICATIONNAME']",
            "description" : ".//*[local-name() = 'DESCRIPTION']",
            "image" : ".//*[local-name() = 'ICONURL']"
        },
        "TabNodes" : ".//*[local-name() = 'privilegemenu']/*[local-name() = 'menu']/*[local-name() = 'Application']",
        "Tabs" : {
            "id" : ".//*[local-name() = 'id']",
            "name" : ".//*[local-name() = 'caption']",
            "description" : ".//*[local-name() = 'description']",
            "icon" : ".//*[local-name() = 'icon']",
            "formurl" : ".//*[local-name() = 'url']",
            "data" : ".//*[local-name() = 'data']"
        },
        "SubTabNodes" : ".//*[local-name() = 'Application']"
    }

    /******************************************** Internally used utility functions *****************************/
    var fillObjectFromXPathObj = function(object, xml, xpathObj) {
        for (var i in xpathObj) {
            var node = cordys.selectXMLNode(xml, xpathObj[i]);
            if (node && node.childElementCount > 0) {
                object[i] = node;
            } else {
                object[i] = node ? cordys.getTextContent(node) : "";
            }
        }
    }
    /******************************************* Classes ********************************************/

    /**
     *  ApplicationData - Object of Application class(private)
     */

    vg.desktop.ApplicationData = (function() {

        //Tab class
        var Tab = function(tabXml) {
            this.tabXml = cordys.loadXMLDocument(cordys.getXML(tabXml));
            this.subTabs = [];
            fillObjectFromXPathObj(this, this.tabXml, xpaths["Tabs"]);
            //Create subtabs
            var subTabNodes = cordys.selectXMLNodes(this.tabXml, xpaths["SubTabNodes"])
            for (var i = 0; i < subTabNodes.length; i++) {
                this.subTabs.push(new SubTab(subTabNodes[i]));
            }
        };

        //SubTab class
        var SubTab = function(subTabXml) {
            this.subTabXml = cordys.loadXMLDocument(cordys.getXML(subTabXml));
            fillObjectFromXPathObj(this, this.subTabXml, xpaths["Tabs"])
        };
        
        var Application = {}
        var applicationData = Application["applicationData"] = CordysRoot.getCurrentApplicationDataObject();
        Application["applicationDetails"] = applicationData.getApplicationDetails();
        var menuXml = applicationData.getPrivilegeMenuXml();
        Application["menuXml"] = cordys.loadXMLDocument(cordys.getXML(menuXml));
        fillObjectFromXPathObj(Application, Application["applicationDetails"], xpaths["Application"]);
        //Creating tabs
        Application["tabs"] = [];
        var tabNodes = cordys.selectXMLNodes(Application["menuXml"], xpaths["TabNodes"]);
        for (var i = 0; i < tabNodes.length; i++) {
            Application["tabs"].push(new Tab(tabNodes[i]));
        }

        return Application;
    })();
})();
